<template>
  <div>
    <TopFilter :list="topFilterList" :cols="3" @filterChange="changeHandle"></TopFilter>

    <FilterTable
      ref="table"
      height="300"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      :uidkey="'dealerCode'"
      :defaultSelections="selectedRow"
      :onSyncTableData="tableChangeFun"
      :onRowSelectChange="selectChangeHandle"
      :onPageChange="handleTablePageChange"
      :onColumnsChange="columns => (this.columns = columns)"
      selectionType="single"
    >
    </FilterTable>
    <!--<FilterTable ref="tables" height="auto" :dataSource="tableList" :columns="columns1" :onColumnsChange="columns => (this.columns1 = columns)" :isSelectColumn="false"> </FilterTable>-->
    <div style="height:47px;"></div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDrawer(1)">关闭</el-button>
      <el-button @click="closeDrawer(2)" type="primary" :disabled="selectedRow.length != 1">确定</el-button>
    </div>
  </div>
</template>
<script>
  import { dealerAttach } from '@/api/omFinance';
  import { findPage } from '@/api/cms-api/origin/originInfo';
  import { dictionary } from '@/utils/dictMixin';
  import { arrayObjDeWeight } from '@/utils';
  import { getRegion} from '@/api/cms-common/dealerdata/index';
  import { dealerCommon } from '@/api/cms-api/origin/commonDealerType'; //服务站类型工具类
  export default {
    data() {
      this.tables = null;
      return {

        topFilterList: this.createdFrom(),
        columns: this.createdTable(),
        columns1: this.createdTables(),
        fetchParams: {
          shopBusinessTypeStr:16261002
        },
        fetchApi: findPage  ,
        selectedRow: [], // 选中的行项目
        totalArrList: [], // 选中的打印项目
        pageSelectRows: [], // 当前页选中的行项(过滤取消勾选项用)
        tableList: [],

      };
    },
    mounted() {
      //初始化事业部下拉数据
      this.tables = this.$refs.tables.$refs.pageTable;
    },

    mixins: [dictionary,dealerCommon],
    methods: {

      // table数据变化
      tableChangeFun(val) {
        //页面数据变化重新赋值选中行项
        this.selectedRow = val.filter(item => !!this.totalArrList.find(x => x.dealerCode == item.dealerCode));
        console.log(this.selectedRow, '页面数据变化重新赋值选中行项');
      },
      // table页码变化
      handleTablePageChange(e) {
        this.pageSelectRows = [];
      },
      // table行项选中变化
      selectChangeHandle(e) {
        // // 跨页跨搜索逻辑
        // // 取消选择的行项
        // let deleteRows = this.pageSelectRows.filter(item => !e.find(x => x.dealerCode == item.dealerCode));
        // console.log(deleteRows, '删除的行项');
        // // 当页选中行项
        // this.pageSelectRows = e;
        //
        // // 从总数据中删除取消勾选的行项
        // this.totalArrList = this.totalArrList.filter(item => !deleteRows.find(x => x.dealerCode == item.dealerCode));
        // // 把选中数据合并到处理后总数组
        // let totalPartList = [...this.totalArrList, ...e];
        // // 根据服务站代码去重
        // this.totalArrList = arrayObjDeWeight(totalPartList, 'dealerCode');
        // // 向选择列表赋值
        // this.tableList = this.totalArrList;
        //
        // console.log(this.totalArrList, '行项选中变化');
        this.selectedRow = e;
        console.log(this.selectedRow, '行项选中变化');
      },
      createdFrom() {
        return [
          {
            type: 'INPUT',
            label: '服务站代码',
            fieldName: 'dealerCode',
            placeholder: '请选择'
          },
          {
            type: 'INPUT',
            label: '服务站名称',
            fieldName: 'dealerName',
            placeholder: '请选择'
          },
          {
            type: 'INPUT',
            label: '服务站简称',
            fieldName: 'dealerShortname',
            initialValue: '',
          },
          { type: 'SELECT',
            label: '网点类型',
            fieldName: 'branchType',
            initialValue: 16231001,
            itemList: this.createDictList('1623'),
            disabled: false,
          },
          // { type: 'SELECT',
          //   label: '类型',
          //   fieldName: 'shopBusinessTypeStr',
          //   initialValue: 16231001,
          //   itemList: this.createDictList('1623'),
          //   disabled: false,
          //   hidden: true,
          // },

        ];
      },
      createdTable() {
        return [
          {
            title: '序号',
            dataIndex: 'index',
            width: 80,
            sorter: true,
            fixed: 'left',
            render: props => {
              return <span>{props.row.index + 1}</span>;
            }
          },
          {
            title: '公司名称',
            dataIndex: 'dealerName',
            sorter: true,
            filter: true,
            filterType: 'input'
          },
          {
            title: '服务站代码',
            dataIndex: 'dealerCode',
            sorter: true,
            filter: true,
            filterType: 'input'
          },
          {
            title: '服务站简称',
            dataIndex: 'dealerShortname',
            sorter: true,
            filter: true,
            filterType: 'input'
          },
          {
            title: '省份',
            dataIndex: 'provinceName',
            sorter: true,
            filter: true,
            filterType: 'input'
          },
          {
            title: '城市',
            dataIndex: 'cityName',
            sorter: true,
            filter: true,
            filterType: 'input'
          },
        ];
      },
      createdTables() {
        return [
          {
            title: '操作',
            dataIndex: 'column-action',
            width: 70,
            fixed: 'left',
            render: props => {
              return (
                <div>
                <el-button size="mini" type="text" onClick={() => this.delFun(props.row)}>
              删除
              </el-button>
              </div>
            );
            }
          },
          {
            title: '服务站代码',
            dataIndex: 'dealerCode',
            sorter: true,
            filter: true,
            filterType: 'input'
          },
          {
            title: '服务站名称',
            dataIndex: 'dealerName',
            sorter: true,
            filter: true,
            filterType: 'input'
          }
        ];
      },
      changeHandle(val) {
        this.fetchParams = Object.assign({}, this.fetchParams, val);
      },
      delFun(data) {
        this.tables.EXECUTE_DELETE([data]);
      },
      closeDrawer(type) {
        if (type == 1) {
          this.$emit('close');
        } else {
          if(this.selectedRow.length != 1){
            this.$notify.warning({title: '提示信息',message:'只能选择一条记录'});
            return;
          }
          this.$emit('close', false, this.selectedRow);
        }
      }
    }
  };
</script>
